//
//  ShowSightsViewController.m
//  MyTravel
//
//  Created by Hunk on 11-6-17.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "ShowSightsViewController.h"
#import "NetworkManager.h"
#import <QuartzCore/QuartzCore.h>
#import "AsyncLoadingImageView.h"
#import "NSString+URLEncoding.h"
#import "MyLoadingView.h"

#define TAG_PAGECONTROL (102)

@interface ShowSightsViewController (Private)

- (void)addSights:(UIScrollView*)scrollView sightArray:(NSArray*)sightArray;

@end

@implementation ShowSightsViewController
@synthesize sightName;

- (void)dealloc
{
    SAFE_RELEASE(sightImageArray)
    SAFE_RELEASE(scrollView)
    SAFE_RELEASE(pageControl)
    SAFE_RELEASE(descriptionTextView)
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = sightName;
    
    sightImageArray = [[NSMutableArray alloc] init];
    
    // Border view
    UIView* borderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
    [borderView setBackgroundColor:[UIColor clearColor]];
    borderView.layer.borderWidth = 2.0f;
    borderView.layer.borderColor = [UIColor brownColor].CGColor;
    [self.view addSubview:borderView];
    [borderView release];
    
    // Init scrollView
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 210)];
    scrollView.pagingEnabled = YES;
    scrollView.userInteractionEnabled = YES;
    scrollView.scrollEnabled = YES;
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    // Page control
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(2, 210, 316, 16)];
    pageControl.backgroundColor = [UIColor brownColor];
    pageControl.hidesForSinglePage = NO;
    [pageControl setTag:TAG_PAGECONTROL];
    [self.view addSubview:pageControl];
    
    // Alpha view hide the page control view
    UIView* alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 210, 320, 16)];
    [alphaView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:alphaView];
    [alphaView release];
    
    // Bg view
    UIImageView* bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 226, 320, 190)];
    UIImage* image = [UIImage imageNamed:@"CellBackground.png"];
    image = [image stretchableImageWithLeftCapWidth:5 topCapHeight:5];
    [bgImageView setImage:image];
    [self.view addSubview:bgImageView];
    [bgImageView release];
    
    // DescriptionTextView TextView
    descriptionTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 227, 320, 186)];
    [descriptionTextView setBackgroundColor:[UIColor clearColor]];
    [descriptionTextView setShowsVerticalScrollIndicator:YES];
    [descriptionTextView setTextColor:TEXTCOLOR];
    [descriptionTextView setFont:[UIFont fontWithName:@"AmericanTypewriter" size:16]];
    [descriptionTextView setTextAlignment:UITextAlignmentLeft];
    [descriptionTextView setEditable:NO];
    [descriptionTextView setText:@""];
    [self.view addSubview:descriptionTextView];

    [[MyLoadingView sharedMyLoadingView] startLoadingAnimation];
    
    // Request
    [[NetworkManager sharedNetworkManager] requestServerForMethod:NetworkRequest_GetJourneySightInfo
                                                   WithParameters:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                   [sightName URLEncodedString], @"sight_name", nil]
                                                 AndCallBackScope:self
                                                    AndSuccessSel:@"onSuccess:"
                                                     AndFailedSel:@"onFailed:"];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Call back
- (void)onSuccess:(id)sender
{
    NSDictionary* resultDic = (NSDictionary*)sender;
    
    NSDictionary* sightInfoDic = [[resultDic objectForKey:@"sightInfo"] objectAtIndex:0];
    
    [sightImageArray removeAllObjects];
    [sightImageArray addObjectsFromArray:[[sightInfoDic objectForKey:@"image"] componentsSeparatedByString:@"[OVER]"]];
    
    [scrollView setContentSize:CGSizeMake(320 * [sightImageArray count], 210)];
    pageControl.numberOfPages = [sightImageArray count];
    
    [self addSights:scrollView sightArray:sightImageArray];
    
    [descriptionTextView setText:[sightInfoDic objectForKey:@"description"]];
}

- (void)onFailed:(id)sender
{
    NSDictionary* resultDic = (NSDictionary*)sender;
    
    // 暂无数据提示
	UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:SORRY_NODATA 
                                                        message:[resultDic objectForKey:@"message"]
                                                       delegate:self
                                              cancelButtonTitle:@"好的"
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

#pragma mark scrollViewDidScroll 上拉刷新
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark addSights:sight:
- (void)addSights:(UIScrollView*)_scrollView sightArray:(NSArray*)sightArray
{
    for(int i = 0; i < [sightArray count]; i++)
    {
        // Bg view
        UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, 210)];
        bgView.backgroundColor = [UIColor clearColor];
        
        UIImageView* bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 210)];
        UIImage* image = [UIImage imageNamed:@"CellBackground.png"];
        image = [image stretchableImageWithLeftCapWidth:5 topCapHeight:5];
        [bgImageView setImage:image];
        [bgView addSubview:bgImageView];
        [bgImageView release];
        
        // Load and add imageView
        AsyncLoadingImageView* asyncLoadingImageView = [[AsyncLoadingImageView alloc] initWithFrame:CGRectMake(10, 5, 300, 200)];
        NSString* imageString = PATH_SIGHTIMAGE([sightName URLEncodedString], [sightArray objectAtIndex:i]);
        //NSLog(@"%d -- %@\n", (i+1), imageString);
        [asyncLoadingImageView loadImageFromURLString:imageString
                                     placeholderImage:nil];
        asyncLoadingImageView.layer.cornerRadius = 8.0;
        asyncLoadingImageView.layer.masksToBounds = YES;
        [bgView addSubview:asyncLoadingImageView];
        [asyncLoadingImageView autorelease];
        
        [_scrollView addSubview:bgView];
        [bgView release];
    }
}

#pragma mark -
#pragma mark scrollViewDidEndDecelerating
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{   
    pageControl.currentPage = _scrollView.contentOffset.x / 320;
}

@end
